package com.itcpeng.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itcpeng.constant.MessageConstant;
import com.itcpeng.entity.PageResult;
import com.itcpeng.entity.QueryPageBean;
import com.itcpeng.entity.Result;
import com.itcpeng.pojo.CheckGroup;
import com.itcpeng.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    //添加检查组
    @PostMapping(value = "/add")
    @PreAuthorize("hasAnyAuthority('MENU_ADD')")
    public Result add(@RequestParam List<Integer> checkitemIds,@RequestBody CheckGroup checkGroup) {
        checkGroupService.add(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //分页查询
    @PostMapping(value = "/findPage")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
       PageResult<CheckGroup> pageResult =  checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }

    //根据id查询
    @GetMapping(value = "/findById")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findById(Integer id) {
        CheckGroup checkItemId = checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkItemId);
    }

    //根据检查组id查询对应的检查项id
    @GetMapping(value = "/findCheckItemIdCheckGroupId")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public List<Integer> findCheckItemIdCheckGroupId(Integer id) {
        List<Integer> list = checkGroupService.findCheckItemIdCheckGroupId(id);
        return list;
    }

    //编辑回显
    @PostMapping(value = "/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupService.edit(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    //删除检查组
    @PostMapping(value = "/deleteAll")
    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")
    public Result deleteAll(Integer id) {
        checkGroupService.deleteAll(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }


    //查询所有
    @GetMapping(value = "/findAll")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findAll() {
        List<CheckGroup> list = checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
    }
}
